#include "lv_port_disp.h"
#include "sdl2_lcd.h"
#include <stdbool.h>

#define MY_DISP_HOR_RES 240
#define MY_DISP_VER_RES 240

volatile bool disp_flush_enabled = true;
static sdl2_lcd lcd;

static void disp_init(void)
{
	lcd.cfg.fix.width = MY_DISP_HOR_RES;
	lcd.cfg.fix.height = MY_DISP_VER_RES;
	sdl2_lcd_init(&lcd);
}

static void disp_flush(lv_disp_drv_t* disp_drv, const lv_area_t* area, lv_color_t* color_p)
{
	if (disp_flush_enabled) {
		sdl2_lcd_fill(&lcd, area->x1, area->y1, area->x2, area->y2, (uint16_t*)color_p);
	}

	lv_disp_flush_ready(disp_drv);
}

void lv_port_disp_init(void)
{
	disp_init();

	static lv_disp_draw_buf_t draw_buf_dsc_3;
	static lv_color_t buf_3_1[MY_DISP_HOR_RES * MY_DISP_VER_RES];
	static lv_color_t buf_3_2[MY_DISP_HOR_RES * MY_DISP_VER_RES];
	lv_disp_draw_buf_init(&draw_buf_dsc_3, buf_3_1, buf_3_2, MY_DISP_VER_RES * MY_DISP_VER_RES);

	static lv_disp_drv_t disp_drv;
	lv_disp_drv_init(&disp_drv);

	disp_drv.hor_res = MY_DISP_HOR_RES;
	disp_drv.ver_res = MY_DISP_VER_RES;
	disp_drv.flush_cb = disp_flush;
	disp_drv.draw_buf = &draw_buf_dsc_3;
	lv_disp_drv_register(&disp_drv);
}

void disp_enable_update(void)
{
	disp_flush_enabled = true;
}

void disp_disable_update(void)
{
	disp_flush_enabled = false;
}
